<?php 


class RealityModel {
	
	const TABLE_REALITY = "reality";
	private $instance;
	
	public function __construct(){
						
	}
	
	public static function createDB(){
		
		dibi::query("CREATE TABLE IF NOT EXISTS `reality` (
  				`id_reality` mediumint(8) unsigned NOT NULL AUTO_INCREMENT,
  				`date` datetime NOT NULL,
  				`name` varchar(255) NOT NULL,
  				`area` varchar(255) NOT NULL,
  				`price` DECIMAL(12,4) NOT NULL,
  				PRIMARY KEY (`id_reality`)
				) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=0;
				");
		
		dibi::query("CREATE TABLE IF NOT EXISTS `reality_photogallery` (
  				`id_photo` mediumint(8) unsigned NOT NULL AUTO_INCREMENT,
  				`title` varchar(255) NOT NULL,
  				`path` varchar(255) NOT NULL,
  				`path_th` varchar(255) NOT NULL,
  				`id_reality` mediumint(8) unsigned NOT NULL,
  				PRIMARY KEY (`id_photo`),
  				KEY (`id_reality`)
				) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=0;
				");
		
		dibi::query("ALTER TABLE `reality_photogallery`
 						 ADD CONSTRAINT `reality_photogallery_ibfk_1` FOREIGN KEY (`id_reality`) REFERENCES `reality` (`id_reality`) ON DELETE CASCADE ON UPDATE CASCADE;");
			
		
	}
	
	public static function addReality($reality){
		
		return dibi::insert(self::TABLE_REALITY , self::realityToDB($reality))
			->execute();
		
	}
	
	private static function realityToDB($reality){
		
		return array(
					"date" => new DateTime(),
					"name" => $reality->getName(),
					"area" => $reality->getArea(),
					"price" => $reality->getPrice(),
		);
	}
	
}

?>